// view/my/diancan/fengl/fengl.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: '',
    title: "分类管理",
    lie: [],
    iszeng: false,
    iddazhe: false,
    fid: 0,
    className: '', //分类名称
    solt: '', //排序
    isShow: '', //0启用 1关闭
    ssfllist: [{
      className: '顶级分类',
      id: 0
    }], //所属分类
    ssflindex: 0,
    ssflguo: '',
    memberList: [],
    xinz: false
  },

  // isOpen: function (e) {
  //   var that = this;
  //   var idx = e.currentTarget.dataset.index;
  //   console.log(idx);
  //   var memberList = that.data.memberList;
  //   console.log(memberList);
  //   for (let i = 0; i < memberList.length; i++) {
  //     if (idx == i) {
  //       memberList[i].hidden = !memberList[i].hidden;
  //     } else {
  //       memberList[i].hidden = true;
  //     }
  //   }
  //   this.setData({
  //     memberList: memberList
  //   });
  //   return true;
  // },
  bindssfl(e) {
    this.setData({
      ssflguo: this.data.ssfllist[e.detail.value].className,
      fid: this.data.ssfllist[e.detail.value].id
    })
  },
  // 分类名称
  className(e) {
    this.setData({
      className: e.detail.value
    })
  },
  // 排序
  solt(e) {
    this.setData({
      solt: e.detail.value
    })
  },
  // 是否显示
  bindazhe() {
    this.setData({
      iddazhe: !this.data.iddazhe
    })
    if (this.data.iddazhe) {
      this.setData({
        isShow: 0
      })
    } else {
      this.setData({
        isShow: 1
      })
    }
  },
  // 确定验证
  que() {
    if (this.data.className == '') {
      wx.showToast({
        title: '请输入分类名称',
        icon: 'none'
      })
    } else if (this.data.solt == '') {
      wx.showToast({
        title: '请输入排序',
        icon: 'none'
      })
    } else {
      this.quedingj()
    }
  },
  // 确定接口
  quedingj() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'hallfood/goodsAddClass', {
      shopId: wx.getStorageSync('shopId'),
      fid: that.data.fid,
      className: that.data.className,
      solt: that.data.solt,
      isShow: that.data.isShow
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      that.setData({
        fid: 0,
        xinz: false
      })
      if (res.data.code == 200) {
        that.setData({
          ssfllist: [{
            className: '顶级分类',
            id: 0
          }], //所属分类
          iddazhe: false,
          fid: 0,
          className: '', //分类名称
          solt: '', //排序
          isShow: '', //0启用 1关闭
        })
        that.canzlist()
        that.setData({
          iszeng: !that.data.iszeng
        })
        setTimeout(function () {
          that.setData({
            buttonClicked: false
          }, 1000)
        })
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 详情
  flxq(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id;
    let fid = e.currentTarget.dataset.fid;
    wx.navigateTo({
      url: '/my_dian/fenglxq/fenglxq?id=' + id + '&fid=' + fid,
    })
  },
  xinzeng() {
    this.setData({
      iszeng: !this.data.iszeng,
      xinz: true
    })
  },
  // 增加
  addfl() {
    this.setData({
      iszeng: !this.data.iszeng,
      xinz: false
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 分类列表
  canzlist(e) {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'hallfood/goodsSelectClass', {
      shopId: wx.getStorageSync('shopId'),
      fid: that.data.fid
    }, 'POST').then(res => {
      wx.hideLoading();
      that.setData({
        memberList: res.data.data.result,
        ssfllist: that.data.ssfllist.concat(res.data.data.result)
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      ssflguo: this.data.ssfllist[this.data.ssflindex].className
    })
    if (this.data.iddazhe) {
      this.setData({
        isShow: 0
      })
    } else {
      this.setData({
        isShow: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      ssfllist: [{
        className: '顶级分类',
        id: 0
      }], //所属分类
    })
    this.canzlist()
    this.setData({
      fid: 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})